package ren.steve.ngh.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ren.steve.annotation.Log;
import ren.steve.common.result.HttpStatus;
import ren.steve.common.result.Result;
import ren.steve.enums.BusinessType;
import ren.steve.model.ngh.bean.NghQueryResult;
import ren.steve.ngh.service.NghQueryResultService;

import java.util.List;

/**
 * @Author: CIS-T-13 Liu GuoJia
 * @Description:
 * @Date: Created in 2024/3/27 11:54
 */

@Api(tags = "APIs-NGH-RESULT标签页")
@CrossOrigin
@RestController
@RequestMapping("/api/ngh/query/result")
public class NghQueryResultController {

  @Autowired
  NghQueryResultService nghQueryResultService;

  @ApiOperation("API-根据TabId获取RESULT")
  @Log(title = "NGH_数据查询_根据TabId获取RESULT", businessType = BusinessType.GET)
  @GetMapping("list")
  public Result listByTabId (Integer tabId) {
    List<NghQueryResult> results = nghQueryResultService.list(tabId);
    return Result.ok(results, HttpStatus.OK);
  }

  @ApiOperation("API-根据Id获取RESULT")
  @Log(title = "NGH_数据查询_根据Id获取RESULT", businessType = BusinessType.GET)
  @GetMapping("details")
  public Result details (Integer id) {
    NghQueryResult result = nghQueryResultService.selectById(id);
    if (result == null) {
      return Result.build(null, HttpStatus.SERVER_DAO_ERROR);
    }
    return Result.ok(result, HttpStatus.OK);
  }

  @ApiOperation("API-添加RESULT")
  @Log(title = "NGH_数据查询_添加RESULT", businessType = BusinessType.INSERT)
  @PostMapping("add")
  public Result insert (@RequestBody NghQueryResult card) {
    Integer tabId = card.getTabId();
    List<NghQueryResult> results = nghQueryResultService.list(tabId);
    if (results.size() < 999) {
      NghQueryResult result = nghQueryResultService.insert(card, results);
      if (result != null) {
        return Result.ok(result, HttpStatus.OK);
      }
      return Result.build(null, HttpStatus.SERVER_DAO_ERROR);
    } else {
      return Result.build(null, HttpStatus.NGH_QUERY_MAX_RESULT);
    }
  }

  @ApiOperation("API-更新RESULT")
  @Log(title = "NGH_数据查询_更新RESULT", businessType = BusinessType.UPDATE)
  @PostMapping("update")
  public Result update (@RequestBody NghQueryResult card) {
    NghQueryResult result = nghQueryResultService.update(card);
    if (result != null) {
      return Result.ok(result, HttpStatus.OK);
    }
    return Result.build(null, HttpStatus.SERVER_DAO_ERROR);
  }

  @ApiOperation("API-删除RESULT")
  @Log(title = "NGH_数据查询_删除RESULT", businessType = BusinessType.DELETE)
  @PostMapping("delete")
  public Result remove (@RequestBody NghQueryResult card) {
    int result = nghQueryResultService.removeById(card.getId());
    if (result != 1) {
      return Result.build(null, HttpStatus.SERVER_DAO_ERROR);
    }
    return Result.ok(null, HttpStatus.OK);
  }
}
